package com.qf.controller;

import com.qf.form.SmsInterfaceForm;
import com.qf.service.SmsInterfaceFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/10/15 20:59
 * description:
 */
@RestController
@RequestMapping("/sms")
@Slf4j
public class SmsController {
    //读取配置文件中被Nginx代理后的请求中的属性名
    @Value("${ip-header}")
    private List<String> ipHeaders;

@Autowired
private SmsInterfaceFormService smsInterfaceFormService;

    /**
     * 接收用户方项目发给Nginx做负载均衡,Nginx将发送的短信数据发送到这里
     * @param smsInterfaceForm 用户发送的对象
     * @param request
     * @return
     */
    @PostMapping("/smsInterface")
    public Map smsInterface(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){

        //1.获取用户请求携带的ip地址
        String ip = getRealIpAddress(request);
        //2.将ip地址放到短信对象中封装
        smsInterfaceForm.setIp(ip);

        //3.对各种参数做非空检验
        Map resultmap = checkNotNull(smsInterfaceForm);
        if (resultmap !=null){
            log.info("[接口模块] 接受用户请求判断参数非空校验失败,resultmap={},smsInterfaceForm={}"+resultmap,smsInterfaceForm);
            return resultmap;
        }
        //4.调用service,进一步做检验,和短信对象中的数据初始化工作
        resultmap = smsInterfaceFormService.checkInterfaceForm(smsInterfaceForm);
        //5.根据service的返回结果反馈给客户
return resultmap;

    }


    /**
     * 校验短信对象属性不为空
     * @param smsInterfaceForm
     * @return
     */
    private  Map checkNotNull(SmsInterfaceForm smsInterfaceForm){
       Map<String,String> resultMap=new HashMap<>();

       //校验客户ID
       if (StringUtils.isEmpty(smsInterfaceForm.getClientID())){
           resultMap.put("code","101");
           resultMap.put("msg","认证错误:clientId错误");
                   return resultMap;
       }

        //下行编号
        if (StringUtils.isEmpty(smsInterfaceForm.getSrcID())){
            resultMap.put("code","106");
            resultMap.put("msg","认证错误:下行编号错误");
            return resultMap;
        }
        //目的手机号, 非空，多个号请用英文逗号隔开，最多100个号码
        if (StringUtils.isEmpty(smsInterfaceForm.getMobile())){
            resultMap.put("code","105");
            resultMap.put("msg","认证错误:手机号错误");
            return resultMap;
        }
        //消息的内容,内容长度<=500字符，以utf-8的方式编码
        if (StringUtils.isEmpty(smsInterfaceForm.getContent())||smsInterfaceForm.getContent().length()>500){
            resultMap.put("code","104");
            resultMap.put("msg","认证错误:内容错误");
            return resultMap;
        }
        //校验密码
        if (StringUtils.isEmpty(smsInterfaceForm.getPwd())){
            resultMap.put("code","102");
            resultMap.put("msg","认证错误:密码错误");
            return resultMap;
        }
        //校验客户ip
        if (StringUtils.isEmpty(smsInterfaceForm.getIp())){
            resultMap.put("code","103");
            resultMap.put("msg","认证错误:ip错误");
            return resultMap;
        }
        return null;
    }




    /**
     * 获取用户方的 IP地址  
     * 这个请求可能被Nginx代理或者做负载均衡
     * @param request
     * @return
     */
    private  String  getRealIpAddress(HttpServletRequest request){
        //遍历Nginx代理请求的属性名
        if (ipHeaders != null) {
            for (String ipHeader : ipHeaders) {
                //判断这个请求头不为空
                if (!StringUtils.isEmpty(ipHeader)&& ipHeader.length()>0) {
                    //获取Nginx代理的真实ip地址
                    String ip = request.getHeader(ipHeader);
                    //判断是否真正拿到了ip
                    if (!StringUtils.isEmpty(ip)&&!"unknown".equalsIgnoreCase(ip)){
                    //返回
                    return ip;
                    }

                }
            }
        }

        //如果循环结束没得到ip,则没被Nginx代理,则正常获取ip地址
        return  request.getRemoteAddr();
        
        
        
    }
    
    
}
